﻿using System;
using System.Data;
using System.Text;
using System.Data.SQLite;
namespace He.DAL
{
	/// <summary>
	/// 数据访问类:order
	/// </summary>
	public partial class order
	{
		public order()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQLite.GetMaxID("id", "he_order"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from he_order");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
			parameters[0].Value = id;

			return DbHelperSQLite.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(He.Model.order model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into he_order(");
			strSql.Append("company_name,link_name,phone,tel,fax,address,email,qq,pro_name,content,add_time,quantity,order_code,is_lock,pos_mark,lang,attr_str,attr_str2,attr_str3,attr_str4,attr_str5,attr_num,attr_num2,attr_num3)");
			strSql.Append(" values (");
			strSql.Append("@company_name,@link_name,@phone,@tel,@fax,@address,@email,@qq,@pro_name,@content,@add_time,@quantity,@order_code,@is_lock,@pos_mark,@lang,@attr_str,@attr_str2,@attr_str3,@attr_str4,@attr_str5,@attr_num,@attr_num2,@attr_num3)");
			strSql.Append(";select LAST_INSERT_ROWID()");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@company_name", DbType.String),
					new SQLiteParameter("@link_name", DbType.String),
					new SQLiteParameter("@phone", DbType.String),
					new SQLiteParameter("@tel", DbType.String),
					new SQLiteParameter("@fax", DbType.String),
					new SQLiteParameter("@address", DbType.String),
					new SQLiteParameter("@email", DbType.String),
					new SQLiteParameter("@qq", DbType.String),
					new SQLiteParameter("@pro_name", DbType.String),
					new SQLiteParameter("@content", DbType.String),
					new SQLiteParameter("@add_time", DbType.String),
					new SQLiteParameter("@quantity", DbType.Int32,8),
					new SQLiteParameter("@order_code", DbType.String),
					new SQLiteParameter("@is_lock", DbType.Int32,8),
					new SQLiteParameter("@pos_mark", DbType.String),
					new SQLiteParameter("@lang", DbType.String),
					new SQLiteParameter("@attr_str", DbType.String),
					new SQLiteParameter("@attr_str2", DbType.String),
					new SQLiteParameter("@attr_str3", DbType.String),
					new SQLiteParameter("@attr_str4", DbType.String),
					new SQLiteParameter("@attr_str5", DbType.String),
					new SQLiteParameter("@attr_num", DbType.Int32,8),
					new SQLiteParameter("@attr_num2", DbType.Int32,8),
					new SQLiteParameter("@attr_num3", DbType.Int32,8)};
			parameters[0].Value = model.company_name;
			parameters[1].Value = model.link_name;
			parameters[2].Value = model.phone;
			parameters[3].Value = model.tel;
			parameters[4].Value = model.fax;
			parameters[5].Value = model.address;
			parameters[6].Value = model.email;
			parameters[7].Value = model.qq;
			parameters[8].Value = model.pro_name;
			parameters[9].Value = model.content;
			parameters[10].Value = model.add_time;
			parameters[11].Value = model.quantity;
			parameters[12].Value = model.order_code;
			parameters[13].Value = model.is_lock;
			parameters[14].Value = model.pos_mark;
			parameters[15].Value = model.lang;
			parameters[16].Value = model.attr_str;
			parameters[17].Value = model.attr_str2;
			parameters[18].Value = model.attr_str3;
			parameters[19].Value = model.attr_str4;
			parameters[20].Value = model.attr_str5;
			parameters[21].Value = model.attr_num;
			parameters[22].Value = model.attr_num2;
			parameters[23].Value = model.attr_num3;

			object obj = DbHelperSQLite.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(He.Model.order model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update he_order set ");
			strSql.Append("company_name=@company_name,");
			strSql.Append("link_name=@link_name,");
			strSql.Append("phone=@phone,");
			strSql.Append("tel=@tel,");
			strSql.Append("fax=@fax,");
			strSql.Append("address=@address,");
			strSql.Append("email=@email,");
			strSql.Append("qq=@qq,");
			strSql.Append("pro_name=@pro_name,");
			strSql.Append("content=@content,");
			strSql.Append("add_time=@add_time,");
			strSql.Append("quantity=@quantity,");
			strSql.Append("order_code=@order_code,");
			strSql.Append("is_lock=@is_lock,");
			strSql.Append("pos_mark=@pos_mark,");
			strSql.Append("lang=@lang,");
			strSql.Append("attr_str=@attr_str,");
			strSql.Append("attr_str2=@attr_str2,");
			strSql.Append("attr_str3=@attr_str3,");
			strSql.Append("attr_str4=@attr_str4,");
			strSql.Append("attr_str5=@attr_str5,");
			strSql.Append("attr_num=@attr_num,");
			strSql.Append("attr_num2=@attr_num2,");
			strSql.Append("attr_num3=@attr_num3");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@company_name", DbType.String),
					new SQLiteParameter("@link_name", DbType.String),
					new SQLiteParameter("@phone", DbType.String),
					new SQLiteParameter("@tel", DbType.String),
					new SQLiteParameter("@fax", DbType.String),
					new SQLiteParameter("@address", DbType.String),
					new SQLiteParameter("@email", DbType.String),
					new SQLiteParameter("@qq", DbType.String),
					new SQLiteParameter("@pro_name", DbType.String),
					new SQLiteParameter("@content", DbType.String),
					new SQLiteParameter("@add_time", DbType.String),
					new SQLiteParameter("@quantity", DbType.Int32,8),
					new SQLiteParameter("@order_code", DbType.String),
					new SQLiteParameter("@is_lock", DbType.Int32,8),
					new SQLiteParameter("@pos_mark", DbType.String),
					new SQLiteParameter("@lang", DbType.String),
					new SQLiteParameter("@attr_str", DbType.String),
					new SQLiteParameter("@attr_str2", DbType.String),
					new SQLiteParameter("@attr_str3", DbType.String),
					new SQLiteParameter("@attr_str4", DbType.String),
					new SQLiteParameter("@attr_str5", DbType.String),
					new SQLiteParameter("@attr_num", DbType.Int32,8),
					new SQLiteParameter("@attr_num2", DbType.Int32,8),
					new SQLiteParameter("@attr_num3", DbType.Int32,8),
					new SQLiteParameter("@id", DbType.Int32,8)};
			parameters[0].Value = model.company_name;
			parameters[1].Value = model.link_name;
			parameters[2].Value = model.phone;
			parameters[3].Value = model.tel;
			parameters[4].Value = model.fax;
			parameters[5].Value = model.address;
			parameters[6].Value = model.email;
			parameters[7].Value = model.qq;
			parameters[8].Value = model.pro_name;
			parameters[9].Value = model.content;
			parameters[10].Value = model.add_time;
			parameters[11].Value = model.quantity;
			parameters[12].Value = model.order_code;
			parameters[13].Value = model.is_lock;
			parameters[14].Value = model.pos_mark;
			parameters[15].Value = model.lang;
			parameters[16].Value = model.attr_str;
			parameters[17].Value = model.attr_str2;
			parameters[18].Value = model.attr_str3;
			parameters[19].Value = model.attr_str4;
			parameters[20].Value = model.attr_str5;
			parameters[21].Value = model.attr_num;
			parameters[22].Value = model.attr_num2;
			parameters[23].Value = model.attr_num3;
			parameters[24].Value = model.id;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from he_order ");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
			parameters[0].Value = id;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from he_order ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public He.Model.order GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,company_name,link_name,phone,tel,fax,address,email,qq,pro_name,content,add_time,quantity,order_code,is_lock,pos_mark,lang,attr_str,attr_str2,attr_str3,attr_str4,attr_str5,attr_num,attr_num2,attr_num3 from he_order ");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
			parameters[0].Value = id;

			He.Model.order model=new He.Model.order();
			DataSet ds=DbHelperSQLite.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					model.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["company_name"]!=null && ds.Tables[0].Rows[0]["company_name"].ToString()!="")
				{
					model.company_name=ds.Tables[0].Rows[0]["company_name"].ToString();
				}
				if(ds.Tables[0].Rows[0]["link_name"]!=null && ds.Tables[0].Rows[0]["link_name"].ToString()!="")
				{
					model.link_name=ds.Tables[0].Rows[0]["link_name"].ToString();
				}
				if(ds.Tables[0].Rows[0]["phone"]!=null && ds.Tables[0].Rows[0]["phone"].ToString()!="")
				{
					model.phone=ds.Tables[0].Rows[0]["phone"].ToString();
				}
				if(ds.Tables[0].Rows[0]["tel"]!=null && ds.Tables[0].Rows[0]["tel"].ToString()!="")
				{
					model.tel=ds.Tables[0].Rows[0]["tel"].ToString();
				}
				if(ds.Tables[0].Rows[0]["fax"]!=null && ds.Tables[0].Rows[0]["fax"].ToString()!="")
				{
					model.fax=ds.Tables[0].Rows[0]["fax"].ToString();
				}
				if(ds.Tables[0].Rows[0]["address"]!=null && ds.Tables[0].Rows[0]["address"].ToString()!="")
				{
					model.address=ds.Tables[0].Rows[0]["address"].ToString();
				}
				if(ds.Tables[0].Rows[0]["email"]!=null && ds.Tables[0].Rows[0]["email"].ToString()!="")
				{
					model.email=ds.Tables[0].Rows[0]["email"].ToString();
				}
				if(ds.Tables[0].Rows[0]["qq"]!=null && ds.Tables[0].Rows[0]["qq"].ToString()!="")
				{
					model.qq=ds.Tables[0].Rows[0]["qq"].ToString();
				}
				if(ds.Tables[0].Rows[0]["pro_name"]!=null && ds.Tables[0].Rows[0]["pro_name"].ToString()!="")
				{
					model.pro_name=ds.Tables[0].Rows[0]["pro_name"].ToString();
				}
				if(ds.Tables[0].Rows[0]["content"]!=null && ds.Tables[0].Rows[0]["content"].ToString()!="")
				{
					model.content=ds.Tables[0].Rows[0]["content"].ToString();
				}
				if(ds.Tables[0].Rows[0]["add_time"]!=null && ds.Tables[0].Rows[0]["add_time"].ToString()!="")
				{
					model.add_time=ds.Tables[0].Rows[0]["add_time"].ToString();
				}
				if(ds.Tables[0].Rows[0]["quantity"]!=null && ds.Tables[0].Rows[0]["quantity"].ToString()!="")
				{
					model.quantity=int.Parse(ds.Tables[0].Rows[0]["quantity"].ToString());
				}
				if(ds.Tables[0].Rows[0]["order_code"]!=null && ds.Tables[0].Rows[0]["order_code"].ToString()!="")
				{
					model.order_code=ds.Tables[0].Rows[0]["order_code"].ToString();
				}
				if(ds.Tables[0].Rows[0]["is_lock"]!=null && ds.Tables[0].Rows[0]["is_lock"].ToString()!="")
				{
					model.is_lock=int.Parse(ds.Tables[0].Rows[0]["is_lock"].ToString());
				}
				if(ds.Tables[0].Rows[0]["pos_mark"]!=null && ds.Tables[0].Rows[0]["pos_mark"].ToString()!="")
				{
					model.pos_mark=ds.Tables[0].Rows[0]["pos_mark"].ToString();
				}
				if(ds.Tables[0].Rows[0]["lang"]!=null && ds.Tables[0].Rows[0]["lang"].ToString()!="")
				{
					model.lang=ds.Tables[0].Rows[0]["lang"].ToString();
				}
				if(ds.Tables[0].Rows[0]["attr_str"]!=null && ds.Tables[0].Rows[0]["attr_str"].ToString()!="")
				{
					model.attr_str=ds.Tables[0].Rows[0]["attr_str"].ToString();
				}
				if(ds.Tables[0].Rows[0]["attr_str2"]!=null && ds.Tables[0].Rows[0]["attr_str2"].ToString()!="")
				{
					model.attr_str2=ds.Tables[0].Rows[0]["attr_str2"].ToString();
				}
				if(ds.Tables[0].Rows[0]["attr_str3"]!=null && ds.Tables[0].Rows[0]["attr_str3"].ToString()!="")
				{
					model.attr_str3=ds.Tables[0].Rows[0]["attr_str3"].ToString();
				}
				if(ds.Tables[0].Rows[0]["attr_str4"]!=null && ds.Tables[0].Rows[0]["attr_str4"].ToString()!="")
				{
					model.attr_str4=ds.Tables[0].Rows[0]["attr_str4"].ToString();
				}
				if(ds.Tables[0].Rows[0]["attr_str5"]!=null && ds.Tables[0].Rows[0]["attr_str5"].ToString()!="")
				{
					model.attr_str5=ds.Tables[0].Rows[0]["attr_str5"].ToString();
				}
				if(ds.Tables[0].Rows[0]["attr_num"]!=null && ds.Tables[0].Rows[0]["attr_num"].ToString()!="")
				{
					model.attr_num=int.Parse(ds.Tables[0].Rows[0]["attr_num"].ToString());
				}
				if(ds.Tables[0].Rows[0]["attr_num2"]!=null && ds.Tables[0].Rows[0]["attr_num2"].ToString()!="")
				{
					model.attr_num2=int.Parse(ds.Tables[0].Rows[0]["attr_num2"].ToString());
				}
				if(ds.Tables[0].Rows[0]["attr_num3"]!=null && ds.Tables[0].Rows[0]["attr_num3"].ToString()!="")
				{
					model.attr_num3=int.Parse(ds.Tables[0].Rows[0]["attr_num3"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,company_name,link_name,phone,tel,fax,address,email,qq,pro_name,content,add_time,quantity,order_code,is_lock,pos_mark,lang,attr_str,attr_str2,attr_str3,attr_str4,attr_str5,attr_num,attr_num2,attr_num3 ");
			strSql.Append(" FROM he_order ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQLite.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM he_order ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            object obj = DbHelperSQLite.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from he_order T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQLite.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@tblName", DbType.VarChar, 255),
					new SQLiteParameter("@fldName", DbType.VarChar, 255),
					new SQLiteParameter("@PageSize", DbType.Int32),
					new SQLiteParameter("@PageIndex", DbType.Int32),
					new SQLiteParameter("@IsReCount", DbType.bit),
					new SQLiteParameter("@OrderType", DbType.bit),
					new SQLiteParameter("@strWhere", DbType.VarChar,1000),
					};
			parameters[0].Value = "he_order";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQLite.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

